var utils = {
  sleep_and_log(duration) {
    var interval = 5;
    while (duration > interval) {
      sleep(interval * 1000);
      duration = duration - interval;
      console.log("还剩:" + duration + "s");
    }
    if (duration > 0) {
      sleep(duration * 1000);
      console.log("sleep_and_log: duration ", duration, "s");
    }
  },
  click_text(content) {
    if (this.exists_text(content)) {
      var uiText = text(content).findOne();
      var bounds = uiText.bounds();
      click(bounds.centerX(), bounds.centerY());
      return uiText;
    } else {
      return false;
    }
  },
  click_text_starts_with(content) {
    if (this.exists_text_starts_with(content)) {
      var uiText = textStartsWith(content).findOne();
      var bounds = uiText.bounds();
      click(bounds.centerX(), bounds.centerY());
      return uiText;
    } else {
      return false;
    }
  },
  exists_text(content) {
    if (text(content).exists()) {
      return text(content).findOne();
    } else {
      return false;
    }
  },
  exists_text_starts_with(content) {
    if (textStartsWith(content).exists()) {
      return textStartsWith(content).findOne();
    } else {
      return false;
    }
  },
  get_center_point(x1, y1, x2, y2) {
    return {
      x: x1 + (x2 - x1) / 2,
      y: y1 + (y2 - y1) / 2,
    };
  },
  click_point(x1, y1, x2, y2) {
    var point = this.get_center_point(x1, y1, x2, y2);
    return click(point.x, point.y);
  },
  click_bounds(bounds) {
    click(bounds.centerX(), bounds.centerY());
  },
  click_ui(ui_item) {
    while (true) {
      if (ui_item && ui_item.clickable()) {
        ui_item.click();
        return true;
      } else if (ui_item) {
        ui_item = ui_item.parent();
      } else {
        return false;
      }
    }
  },
  click_x_y(x, y) {
    click(x, y);
  },
  swipe_up(duration, distance) {
    while (duration > 3) {
      swipe(
        device.width / 2,
        device.height / 2,
        device.width / 2,
        device.height / 2 - distance,
        500 + random(0, 10)
      );
      var interval = 1 + random(0, 2);
      sleep(interval * 1000);
      duration = duration - interval;
    }
    if (duration > 0) {
      sleep(duration * 1000);
    }
    utils.toast("swipe_up end");
  },
  toast(content) {
    console.log(content);
    toast(content);
    sleep(6 * 1000);
  },
  sleep(duration) {
    var duration = duration / 1000;
    while (duration > 4) {
      sleep(4 * 1000);
      duration = duration - 4;
    }
    if (duration > 0) {
      sleep(duration * 1000);
    }
  },
  get_gold(content) {
    var reg_exp = /(\d+)/g;
    var gold_match = content.match(reg_exp);
    var gold = parseInt(gold_match ? gold_match[0] : 0);
    return gold;
  },
  get_size_point(points) {
    var length = points.length;
    var size_point = [];
    var rate_x = device.width / 720;
    var rate_y = device.height / 1650;
    for (var i = 0; i < length; i++) {
      var x_y = points[i];
      var rate = i % 2 == 0 ? rate_x : rate_y;
      size_point.push(parseInt(x_y * rate));
    }
    return size_point;
  },
  get_size_point_red_mi(points) {
    var length = points.length;
    var size_point = [];
    var rate_x = device.width / 1080;
    var rate_y = device.height / 2340;
    for (var i = 0; i < length; i++) {
      var x_y = points[i];
      var rate = i % 2 == 0 ? rate_x : rate_y;
      size_point.push(parseInt(x_y * rate));
    }
    return size_point;
  },
};

module.exports=utils